﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

[MaMvc(controllerDescription: "后台首页", PCode = "HtAdmin")]
public class HomeController : HtAdminBaseController
{
    private readonly ISysCacheService _sysCacheService;
    private readonly IHtAccountService _htAccountService;
    public HomeController(ISysCacheService sysCacheService, IHtAccountService htAccountService)
    {
        _sysCacheService = sysCacheService;
        _htAccountService = htAccountService;
    }

    [MaMenu(name: "后台首页", IsShow = false)]
    [MaPermission(name: "浏览", actions: "Index,GetMenus")]
    public async Task<IActionResult> Index()
    {
        //var a = await _htPlatformProjectTestService.GetPlatformProjectSelectItems();

        ViewData["SysSetting"] = await _sysCacheService.GetSetting<SysSettingDto>(SysDataConfigKey.SysSetting);
        var admindto = await _htAccountService.GetLoginUser();

        return View(admindto);
    }

    /// <summary>
    /// 控制台
    /// </summary>
    /// <returns></returns>
    [MaPermission(name: "控制台", actions: "Console")]
    public async Task<IActionResult> Console()
    {
        ViewBag.StartDate = $"{DateTime.Now:yyyy-MM}-01";
        ViewBag.EndDate = $"{DateTime.Now:yyyy-MM-dd}";
        //获取服务器信息
        ViewBag.MachineInfo = await MachineUtil.GetMachineBaseInfo();
        return View();
    }

    [HttpGet]
    [UnifyResult(typeof(JsonResult))]
    [AllowAnonymous]
    public async Task<JsonResult> GetMenus()
    {
        var pagedlist = await _htAccountService.GetMenus("HtAdmin");
        return Json(pagedlist);
    }

    /// <summary>
    /// 刷新缓存
    /// </summary>
    /// <returns></returns>
    [MaPermission(name: "刷新缓存", actions: "RefreshCache")]
    public async Task<JsonResult> RefreshCache()
    {
        await _sysCacheService.RefreshCache();
        return Json("");
    }
}